/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rpt.lib;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Roberto
 */
public class CTabbedPaneWithCloseIcons extends JTabbedPane implements MouseListener {
  public CTabbedPaneWithCloseIcons() {
    super();
    addMouseListener(this);
  }

    @Override
  public void addTab(String title, Component component) {
    this.addTab(title, component, null);
  }

  public void addTab(String title, Component component, Icon extraIcon) {
    super.addTab(title, new CCloseTabIcon(extraIcon), component);
  }

  public void mouseClicked(MouseEvent e) {
    int tabNumber=getUI().tabForCoordinate(this, e.getX(), e.getY());
    if (tabNumber < 0) return;
    Rectangle rect=((CCloseTabIcon)getIconAt(tabNumber)).getBounds();
    if (rect.contains(e.getX(), e.getY())) {
      //the tab is being closed
      this.removeTabAt(tabNumber);
    }
  }

  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
}